Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent conflicting AMBuild installations. #75

Merged
merged 1 commit into from
Aug 1, 2020
Merged

Conversation

dvander
Copy link
Member

@dvander dvander commented Aug 1, 2020

This detects older AMBuild installs that may have been installed with
distutils, and forces their removal.

This detects older AMBuild installs that may have been installed with
distutils, and forces their removal.
import ambuild2.util
except:
sys.exit(2)
if getattr(ambuild2.util, 'INSTALLED_BY_PIP_OR_SETUPTOOLS', False):
Copy link
Contributor

@WildCard65 WildCard65 Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional A: Use a sentinel object

sentinel = object() # Plain object for minimal memory usage.
if getattr(ambuild2.util, 'INSTALLED_BY_PIP_OR_SETUPTOOLS', sentinel) is not sentinel:

Option B: Catch "AttributeError"

try:
  obj = getattr(ambuild2.util, 'INSTALLED_BY_PIP_OR_SETUPTOOLS')
except AttributeError:
  pass
else:
  sys.exit(2)

Option A is less weird looking, Option B is more "pythonic"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check:
Python 2.7+ confirms "object()" results in new objects, not a cached object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to keep this simple - Python is... not my first choice anymore, and maintaining AMBuild is hard. I have to remember a lot of quirks. It should have been written in Go, but that wasn't around when AMBuild was needed. Anyway, "True" is a lot more clear than "object()", especially given the boolean phrasing of the variable name.

sys.stderr.write("do this by inspecting the following locations and removing\n")
sys.stderr.write("any ambuild folders:\n\n")
for path in sys.path[1:]:
candidates = [ os.path.join(path, "ambuild"), os.path.join(path, "ambuild2") ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicks:

  • Use a tuple, candidates is implied to be immutable.
  • A generator expression would also suffice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I try to be consistent in using lists over tuples. Tuples are quirky. It's not clear why most code would care about immutability.

@WildCard65
Copy link
Contributor

Other than the changes I suggested (Nitpick review not included), I'm fine with doing this @dvander.

@dvander
Copy link
Member Author

dvander commented Aug 1, 2020

Thanks for taking a look, hopefully no one runs into any problems.

@dvander dvander merged commit 7bf65dc into master Aug 1, 2020
@dvander dvander deleted the detect-distutils branch August 1, 2020 03:47
@WildCard65
Copy link
Contributor

@dvander It seems you're using multiprocessing wrong according to my local Python install:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Program Files\Python\3.8\lib\runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Program Files\Python\3.8\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Program Files\Python\3.8\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "L:\GIT\ambuild\setup.py", line 25, in <module>
    proc.start()
  File "C:\Program Files\Python\3.8\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files\Python\3.8\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

@WildCard65
Copy link
Contributor

According to the docs, you need to insert a 'freeze_support()' call before creating the Process object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants