def upgrade_file_listing_urls(status):
    filename = '%s.md' % (status)

    articles = []
    with codecs.open(filename, 'r', encoding='utf-8') as file_obj:
        articles = file_mod.read_items_from_file_listing(file_obj.read())

    new_sections = []
    for article in articles:
        stacks = article.stacks if article.stacks else [u'other']
        name = article.author_real_name or article.author_name

        query_str = u'' if status == PUBLISHED else u'?status=%s' % (status)
        url = u'%s/%s/%s%s' % (BASE_URL, utils.slugify_stack(
            stacks[0]), utils.slugify(article.title), query_str)

        author_url = u'%s/user/%s' % (BASE_URL, article.author_name)

        section = file_mod.get_updated_file_listing_text(
            u'', url, article.title, author_url, name, article.author_img_url,
            article.thumbnail_url, stacks)

        new_sections.append(section)

    with codecs.open(filename, 'w', encoding='utf-8') as file_obj:
        file_obj.write(u'\n\n'.join(new_sections))

    cmd = u'git add %s' % (filename)
    subprocess.check_call(cmd.split())

    cmd = u'git commit -m'.split()
    cmd.append(u'Updating URLs in %s' % (filename))
    subprocess.check_call(cmd)
def move_guides():
    """
    Move all guide directories into the proper publish status directory

    All guides will be moved from their current location into a structure like
    the following:
        <publish_status>/<stack>/<directory_name>

    The <publish_status> is one of STATUSES and is determined by the guides'
    metadata 'publish_status' field.

    The <stack> is the first stack in the guides' metadata file.

    The <directory_name> is the same directory as the guide used before.

    Assumes the current working directory is root of git repo.
    """

    for md_file in metadata_files():
        curr_dir = os.path.dirname(md_file)

        top_dir = None
        for dir_ in curr_dir.split(os.sep):
            if dir_ == '.':
                continue

            top_dir = dir_
            break

        if top_dir in STATUSES:
            # guide already in the right directory
            continue

        metadata = json_from_file(md_file)
        stack = metadata['stacks'][0]

        new_dir = os.path.join(metadata['publish_status'],
                               utils.slugify_stack(stack),
                               curr_dir)

        dirname = os.path.dirname(new_dir)

        try:
            os.makedirs(dirname)
        except OSError:
            if not os.path.isdir(dirname):
                raise

        cmd = u'git mv %s %s' % (curr_dir, new_dir)
        subprocess.check_call(cmd.split())

    cmd = u'git commit -m'.split()
    cmd.append(u'Moving guides based on where they are in publish workflow')
    subprocess.check_call(cmd)
def move_guides():
    """
    Move all guide directories into the proper publish status directory

    All guides will be moved from their current location into a structure like
    the following:
        <publish_status>/<stack>/<directory_name>

    The <publish_status> is one of STATUSES and is determined by the guides'
    metadata 'publish_status' field.

    The <stack> is the first stack in the guides' metadata file.

    The <directory_name> is the same directory as the guide used before.

    Assumes the current working directory is root of git repo.
    """

    for md_file in metadata_files():
        curr_dir = os.path.dirname(md_file)

        top_dir = None
        for dir_ in curr_dir.split(os.sep):
            if dir_ == '.':
                continue

            top_dir = dir_
            break

        if top_dir in STATUSES:
            # guide already in the right directory
            continue

        metadata = json_from_file(md_file)
        stack = metadata['stacks'][0]

        new_dir = os.path.join(metadata['publish_status'],
                               utils.slugify_stack(stack), curr_dir)

        dirname = os.path.dirname(new_dir)

        try:
            os.makedirs(dirname)
        except OSError:
            if not os.path.isdir(dirname):
                raise

        cmd = u'git mv %s %s' % (curr_dir, new_dir)
        subprocess.check_call(cmd.split())

    cmd = u'git commit -m'.split()
    cmd.append(u'Moving guides based on where they are in publish workflow')
    subprocess.check_call(cmd)
def upgrade_file_listing_urls(status):
    filename = '%s.md' % (status)

    articles = []
    with codecs.open(filename, 'r', encoding='utf-8') as file_obj:
        articles = file_mod.read_items_from_file_listing(file_obj.read())

    new_sections = []
    for article in articles:
        stacks = article.stacks if article.stacks else [u'other']
        name = article.author_real_name or article.author_name

        query_str = u'' if status == PUBLISHED else u'?status=%s' % (status)
        url = u'%s/%s/%s%s' % (BASE_URL, utils.slugify_stack(stacks[0]),
                               utils.slugify(article.title), query_str)

        author_url = u'%s/user/%s' % (BASE_URL, article.author_name)

        section = file_mod.get_updated_file_listing_text(u'',
                                                         url,
                                                         article.title,
                                                         author_url,
                                                         name,
                                                         article.author_img_url,
                                                         article.thumbnail_url,
                                                         stacks)

        new_sections.append(section)

    with codecs.open(filename, 'w', encoding='utf-8') as file_obj:
        file_obj.write(u'\n\n'.join(new_sections))

    cmd = u'git add %s' % (filename)
    subprocess.check_call(cmd.split())

    cmd = u'git commit -m'.split()
    cmd.append(u'Updating URLs in %s' % (filename))
    subprocess.check_call(cmd)