Exemplo n.º 1
0
 def save(self):
     """
     Store the txdata dictionary in the .tx/txdata file of the project.
     """
     fh = open(self.txdata_file,"w")
     fh.write(compile_json(self.txdata, indent=4))
     fh.close()
Exemplo n.º 2
0
def _cmd_init(argv, path_to_tx=None):
    """
    Initialize the tx client folder. 
    
    The .tx folder is created by default to the CWD!
    """
    # Current working dir path
    root = os.getcwd()

    if path_to_tx:
        if not os.path.exists(path_to_tx):
            print "tx: The path to root directory does not exist!"
            return

        path = find_dot_tx(path_to_tx)
        if path:
            print "tx: There is already a tx folder!"
            reinit = raw_input("Do you want to delete it and reinit the project? [y/N]:")
            while (reinit != 'y' and reinit != 'Y' and reinit != 'N' and reinit != 'n' and reinit != ''):
                reinit = raw_input("Do you want to delete it and reinit the project? [y/N]:")
            if not reinit or reinit == 'N':
                return
            # Clean the old settings
            # FIXME: take a backup
            else:
                rm_dir = os.path.join(path, ".tx")
                shutil.rmtree(rm_dir)

        root = path_to_tx
        print "Creating .tx folder ..."
        # FIXME: decide the mode of the directory
        os.mkdir(os.path.join(path_to_tx,".tx"))

    else:
        path = find_dot_tx(root)
        if path:
            print "tx: There is already a tx folder!"
            reinit = raw_input("Do you want to delete it and reinit the project? [y/N]:")
            while (reinit != 'y' and reinit != 'Y' and reinit != 'N' and reinit != 'n' and reinit != ''):
                reinit = raw_input("Do you want to delete it and reinit the project? [y/N]:")
            if not reinit or reinit == 'N':
                return
            # Clean the old settings 
            # FIXME: take a backup
            else:
                rm_dir = os.path.join(path, ".tx")
                shutil.rmtree(rm_dir)

        print "Creating .tx folder ..."
        # FIXME: decide the mode of the directory
        os.mkdir(os.path.join(os.getcwd(), ".tx"))
    print "Done."

    # Handle the credentials through transifexrc
    home = os.getenv('USERPROFILE') or os.getenv('HOME')
    txrc = os.path.join(home, ".transifexrc")
    config = ConfigParser.RawConfigParser()
    # Touch the file if it doesn't exist
    if not os.path.exists(txrc):
        username = raw_input("Please enter your transifex username :"******"Please enter your transifex username :"******"Creating .transifexrc file ..."
        config.add_section('API credentials')
        config.set('API credentials', 'username', username)
        config.set('API credentials', 'password', passwd)
        config.set('API credentials', 'token', '')

        # Writing our configuration file to 'example.cfg'
        fh = open(txrc, 'w')
        config.write(fh)
        fh.close()
    else:
        print "Read .transifexrc file ..."
        # FIXME do some checks :)
        config.read(txrc)
        username = config.get('API credentials', 'username')
        passwd = config.get('API credentials', 'password')
        token = config.get('API credentials', 'token')
    print "Done."


    # The path to the txdata file (.tx/txdata)
    txdata_file = os.path.join(root, ".tx", "txdata")
    # Touch the file if it doesn't exist
    if not os.path.exists(txdata_file):
        print "Creating txdata file ..."
        open(txdata_file, 'w').close()
        print "Done."

    # Get the project slug
    project_url = raw_input("Please enter your tx project url here :")
    hostname, project_slug = parse_tx_url(project_url)
    while (not hostname and not project_slug):
        project_url = raw_input("Please enter your tx project url here :")
        hostname, project_slug = parse_tx_url(project_url)

    # Check the project existence
    project_info = get_project_info(hostname, username, passwd, project_slug)
    if not project_info:
        # Clean the old settings 
        # FIXME: take a backup
        rm_dir = os.path.join(root, ".tx")
        shutil.rmtree(rm_dir)
        return

    # Write the skeleton dictionary
    print "Creating skeleton ..."
    txdata = { 'resources': [],
               'meta': { 'project_name': project_info['name'],
                         'project_slug': project_info['slug'],
                         'last_push': None} 
             }
    fh = open(txdata_file,"w")
    fh.write(compile_json(txdata, indent=4))
    fh.close()

    # Writing hostname for future usages
    config.read(txrc)
    config.set('API credentials', 'hostname', hostname)
    fh = open(txrc, 'w')
    config.write(fh)
    fh.close()
    print "Done."