def get_conf_file(pacman): """Return the configuration file of the given package manager. """ conf = REQUIREMENTS_FILES.get(pacman.lower()) if not conf: print("There is no configuration file for this package manager. Abort.") return None return conf
def main(pm="", message="", dest="", rm=False, editor=False, init=False, *packages): """syp will check what's new in your config files, take the arguments into account, and it will install and remove packages accordingly. It uses a cache in ~/.syp/. pm: set the package manager, according to your settings. If not specified, it will work on all of them. message: comment to be written in the configuration file. dest: <not implemented> rm: remove the given packages. If no package is specified, call $EDITOR on the configuration file. editor: call your shell's $EDITOR to edit the configuration file associated to the given package manager, before the rest. init: write the default settings to ~/.syp/settings.py TODO: give list of available pacman. Check your settings in ~/syp/settings.py. Create them with syp --init. """ root_dir = REQUIREMENTS_ROOT_DIR req_files = REQUIREMENTS_FILES.items() req_files = list(REQUIREMENTS_FILES.items()) if init: check_conf_dir("~/.syp/") if not os.path.isfile(cfg_file): copy_file("settings.py", cfg_file) print("Done.") else: print("warning: the file {} already exists. Do nothing.".format(CFG_FILE)) exit(0) # Deal with a specific package manager if pm: # Sync only the conf file of the current package manager. req_files = [tup for tup in req_files if tup[0] == pm] print("Let's use {} to install packages {} !".format(pm, " ".join(packages))) conf_file = get_conf_file(pm) if not conf_file: exit(1) if editor: run_editor(root_dir, conf_file) # TODO: venv if not rm: write_packages(packages, message=message, conf_file=conf_file, root_dir=root_dir) else: if not packages: # Edit conf file directly conf = expanduser(os.path.join(root_dir, conf_file)) cmd = " ".join([os.environ.get('EDITOR'), conf]) ret = os.system(cmd) # Edit the file in cache. erase_packages(packages, message=message, conf_file=conf_file, root_dir=root_dir) print("Syncing {} packages...".format(pm.lower())) if dest: # We could be in a venv but in the root of anothe project and still # add a package to its requirement, that we give on the cli. print("destination to write: ", dest) exit # Do the job: check_conf_dir() ret_codes = [] for _, val in req_files: ret_codes.append(sync_packages(val, root_dir=root_dir)) return reduce(operator.or_, ret_codes, 0)