Пример #1
0
def _find_or_create_user_id():
    """
    The user's ID is stored on a file under the global config directory.

    The file should contain a JSON with a "user_id" key:

        {"user_id": "16fd2706-8baf-433b-82eb-8c7fada847da"}

    IDs are generated randomly with UUID.
    """
    config_dir = Config.get_global_config_dir()
    fname = os.path.join(config_dir, "user_id")
    lockfile = os.path.join(config_dir, "user_id.lock")

    # Since the `fname` and `lockfile` are under the global config,
    # we need to make sure such directory exist already.
    makedirs(config_dir, exist_ok=True)

    try:
        with Lock(lockfile):
            try:
                with open(fname, "r") as fobj:
                    user_id = json.load(fobj)["user_id"]

            except (FileNotFoundError, ValueError, KeyError):
                user_id = str(uuid.uuid4())

                with open(fname, "w") as fobj:
                    json.dump({"user_id": user_id}, fobj)

            return user_id

    except LockError:
        logger.debug(
            "Failed to acquire '{lockfile}'".format(lockfile=lockfile))
Пример #2
0
    def __init__(self, info={}):
        from dvc.config import Config
        from dvc.lock import Lock

        self.info = info

        cdir = Config.get_global_config_dir()
        try:
            os.makedirs(cdir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        self.user_id_file = os.path.join(cdir, self.USER_ID_FILE)
        self.user_id_file_lock = Lock(cdir, self.USER_ID_FILE + '.lock')