def untrack(self, path): dst_path = utils.truepath(path) if not os.path.exists(dst_path): raise Exception("Path '%s' not found" % dst_path) if not os.path.islink(dst_path): raise Exception("Path '%s' is not a symlink" % dst_path) src_path = os.path.realpath(dst_path) undo_log = [] utils.remove_symlink(dst_path, dry_run=self.dry_run, undo_log=undo_log) try: utils.rename(src_path, dst_path, dry_run=self.dry_run) except: utils.undo_operations(undo_log) raise self.git.rm(src_path) self.git.commit(message="Untracking '%s'" % path)
def track(self, path, bundle=None): """Track a file or a directory.""" # We don't use kwarg default, because None represents default to # callers bundle = bundle or 'Default' src_path = utils.truepath(path) is_directory = os.path.isdir(src_path) if self.root_path not in src_path: raise Exception('Cannot track files outside of root path') bundle_path = os.path.join(self.repo_path, bundle) dst_path = os.path.join( bundle_path, utils.relpath(self.root_path, src_path)) undo_log = [] dst_dir = os.path.dirname(dst_path) if not os.path.exists(dst_dir): utils.makedirs(dst_dir, dry_run=self.dry_run, undo_log=undo_log) try: utils.rename(src_path, dst_path, dry_run=self.dry_run, undo_log=undo_log) try: utils.symlink(dst_path, src_path, dry_run=self.dry_run) except: utils.undo_operations(undo_log) raise except: utils.undo_operations(undo_log) raise self.git.add(dst_path) if is_directory: marker = self._track_directory(dst_path) self.git.add(marker) self.git.commit(message="Tracking '%s'" % path)
def main(): parser = optparse.OptionParser(usage()) parser.add_option("-d", "--dry-run", action="store_true", dest="dry_run", default=False, help="Don't actually make the changes") parser.add_option("-b", "--bundle", action="store", dest="bundle", help="Which bundle to use") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Turns on verbose output.") parser.add_option("--version", action="store_true", dest="version", default=False, help="Print version and exit") options, args = parser.parse_args() if options.version: print version.__version__ return # --dry-run implies --verbose utils.LOG_VERBOSE = options.verbose or options.dry_run repo_path = utils.truepath(os.getenv('HOMEFILES_REPO') or DEFAULT_REPO) root_path = utils.truepath(os.getenv('HOMEFILES_ROOT') or DEFAULT_ROOT) remote_repo = os.getenv('HOMEFILES_REMOTE_REPO') or DEFAULT_REMOTE_REPO hf = homefiles.Homefiles(root_path, repo_path, remote_repo, dry_run=options.dry_run) try: cmd = args[0] except IndexError: parser.print_help() print >> sys.stderr, usage() sys.exit(1) if cmd == 'bundles': matching, non_matching = hf.bundle_breakdown() print 'Match this machine:' for bundle in sorted(matching): print '- %s' % bundle print print 'Others:' for bundle in sorted(non_matching): print '- %s' % bundle elif cmd == 'clone': try: origin = args[1] except IndexError: print >> sys.stderr, usage() sys.exit(1) try: hf.clone(origin) except homefiles.HomefilesException as e: utils.error(e) sys.exit(1) elif cmd == 'diff': try: hf.diff() except homefiles.HomefilesException as e: utils.error(e) sys.exit(1) elif cmd == 'init': hf.init() elif cmd == 'link': if options.bundle: selected = [s.strip() for s in options.bundle.split(',')] else: selected = None try: hf.link(selected=selected) except homefiles.HomefilesException as e: utils.error(e) sys.exit(1) elif cmd == 'sync': try: message = args[1] except IndexError: message = 'Sync' hf.sync(message=message) elif cmd == 'track': try: path = args[1] except IndexError: print >> sys.stderr, usage() sys.exit(1) hf.track(path, bundle=options.bundle) elif cmd == 'unlink': try: hf.unlink() except homefiles.HomefilesException as e: utils.error(e) sys.exit(1) elif cmd == 'untrack': try: path = args[1] except IndexError: print >> sys.stderr, usage() sys.exit(1) hf.untrack(path) else: print >> sys.stderr, "error: Unrecognized command '%s'" % cmd print >> sys.stderr, usage() sys.exit(1)