Skip to content

Commit

Permalink
[build] Extend use of devscripts/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkf committed Jul 25, 2023
1 parent a25e9f3 commit b870181
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 74 deletions.
11 changes: 7 additions & 4 deletions devscripts/bash-completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
from os.path import dirname as dirn
import sys

sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

import youtube_dl
from youtube_dl.compat import compat_open as open

from utils import read_file

BASH_COMPLETION_FILE = "youtube-dl.bash-completion"
BASH_COMPLETION_TEMPLATE = "devscripts/bash-completion.in"
Expand All @@ -18,9 +22,8 @@ def build_completion(opt_parser):
for option in group.option_list:
# for every long flag
opts_flag.append(option.get_opt_string())
with open(BASH_COMPLETION_TEMPLATE) as f:
template = f.read()
with open(BASH_COMPLETION_FILE, "w") as f:
template = read_file(BASH_COMPLETION_TEMPLATE)
with open(BASH_COMPLETION_FILE, "w", encoding='utf-8') as f:
# just using the special char
filled_template = template.replace("{{flags}}", " ".join(opts_flag))
f.write(filled_template)
Expand Down
9 changes: 5 additions & 4 deletions devscripts/create-github-release.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
from __future__ import unicode_literals

import io
import json
import mimetypes
import netrc
Expand All @@ -10,7 +9,9 @@
import re
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

from youtube_dl.compat import (
compat_basestring,
Expand All @@ -22,6 +23,7 @@
make_HTTPS_handler,
sanitized_Request,
)
from utils import read_file


class GitHubReleaser(object):
Expand Down Expand Up @@ -89,8 +91,7 @@ def main():

changelog_file, version, build_path = args

with io.open(changelog_file, encoding='utf-8') as inf:
changelog = inf.read()
changelog = read_file(changelog_file)

mobj = re.search(r'(?s)version %s\n{2}(.+?)\n{3}' % version, changelog)
body = mobj.group(1) if mobj else ''
Expand Down
11 changes: 6 additions & 5 deletions devscripts/fish-completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
from os.path import dirname as dirn
import sys

sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

import youtube_dl
from youtube_dl.utils import shell_quote

from utils import read_file, write_file

FISH_COMPLETION_FILE = 'youtube-dl.fish'
FISH_COMPLETION_TEMPLATE = 'devscripts/fish-completion.in'

Expand Down Expand Up @@ -38,11 +41,9 @@ def build_completion(opt_parser):
complete_cmd.extend(EXTRA_ARGS.get(long_option, []))
commands.append(shell_quote(complete_cmd))

with open(FISH_COMPLETION_TEMPLATE) as f:
template = f.read()
template = read_file(FISH_COMPLETION_TEMPLATE)
filled_template = template.replace('{{commands}}', '\n'.join(commands))
with open(FISH_COMPLETION_FILE, 'w') as f:
f.write(filled_template)
write_file(filled_template)


parser = youtube_dl.parseOpts()[0]
Expand Down
15 changes: 10 additions & 5 deletions devscripts/gh-pages/add-version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import hashlib
import os.path

dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(dirn(os.path.abspath(__file__)))))

from devscripts.utils import read_file, write_file
from youtube_dl.compat import compat_open as open

if len(sys.argv) <= 1:
print('Specify the version number as parameter')
sys.exit()
version = sys.argv[1]

with open('update/LATEST_VERSION', 'w') as f:
f.write(version)
write_file('update/LATEST_VERSION', version)

versions_info = json.load(open('update/versions.json'))
versions_info = json.loads(read_file('update/versions.json'))
if 'signature' in versions_info:
del versions_info['signature']

Expand All @@ -39,5 +44,5 @@
versions_info['versions'][version] = new_version
versions_info['latest'] = version

with open('update/versions.json', 'w') as jsonf:
json.dump(versions_info, jsonf, indent=4, sort_keys=True)
with open('update/versions.json', 'w', encoding='utf-8') as jsonf:
json.dumps(versions_info, jsonf, indent=4, sort_keys=True)
17 changes: 12 additions & 5 deletions devscripts/gh-pages/generate-download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
from __future__ import unicode_literals

import json
import os.path
import sys

versions_info = json.load(open('update/versions.json'))
dirn = os.path.dirname

sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))

from utils import read_file, write_file

versions_info = json.loads(read_file('update/versions.json'))
version = versions_info['latest']
version_dict = versions_info['versions'][version]

# Read template page
with open('download.html.in', 'r', encoding='utf-8') as tmplf:
template = tmplf.read()
template = read_file('download.html.in')

template = template.replace('@PROGRAM_VERSION@', version)
template = template.replace('@PROGRAM_URL@', version_dict['bin'][0])
Expand All @@ -18,5 +25,5 @@
template = template.replace('@EXE_SHA256SUM@', version_dict['exe'][1])
template = template.replace('@TAR_URL@', version_dict['tar'][0])
template = template.replace('@TAR_SHA256SUM@', version_dict['tar'][1])
with open('download.html', 'w', encoding='utf-8') as dlf:
dlf.write(template)

write_file('download.html', template)
17 changes: 11 additions & 6 deletions devscripts/gh-pages/update-copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@

import datetime
import glob
import io # For Python 2 compatibility
import os
import re
import sys

year = str(datetime.datetime.now().year)
dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(dirn(os.path.abspath(__file__)))))

from devscripts.utils import read_file, write_file
from youtube_dl import compat_str

year = compat_str(datetime.datetime.now().year)
for fn in glob.glob('*.html*'):
with io.open(fn, encoding='utf-8') as f:
content = f.read()
content = read_file(fn)
newc = re.sub(r'(?P<copyright>Copyright © 2011-)(?P<year>[0-9]{4})', 'Copyright © 2011-' + year, content)
if content != newc:
tmpFn = fn + '.part'
with io.open(tmpFn, 'wt', encoding='utf-8') as outf:
outf.write(newc)
write_file(tmpFn, newc)
os.rename(tmpFn, fn)
11 changes: 8 additions & 3 deletions devscripts/gh-pages/update-feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from __future__ import unicode_literals

import datetime
import io
import json
import os.path
import textwrap
import sys

dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

from utils import write_file

atom_template = textwrap.dedent("""\
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -72,5 +78,4 @@
entries_str = textwrap.indent(''.join(entries), '\t')
atom_template = atom_template.replace('@ENTRIES@', entries_str)

with io.open('update/releases.atom', 'w', encoding='utf-8') as atom_file:
atom_file.write(atom_template)
write_file('update/releases.atom', atom_template)
11 changes: 6 additions & 5 deletions devscripts/gh-pages/update-sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import os
import textwrap

dirn = os.path.dirname

# We must be able to import youtube_dl
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
sys.path.insert(0, dirn(dirn(dirn(os.path.abspath(__file__)))))

import youtube_dl
from devscripts.utils import read_file, write_file


def main():
with open('supportedsites.html.in', 'r', encoding='utf-8') as tmplf:
template = tmplf.read()
template = read_file('supportedsites.html.in')

ie_htmls = []
for ie in youtube_dl.list_extractors(age_limit=None):
Expand All @@ -29,8 +31,7 @@ def main():

template = template.replace('@SITES@', textwrap.indent('\n'.join(ie_htmls), '\t'))

with open('supportedsites.html', 'w', encoding='utf-8') as sitesf:
sitesf.write(template)
write_file('supportedsites.html', template)


if __name__ == '__main__':
Expand Down
9 changes: 4 additions & 5 deletions devscripts/make_contributing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
from __future__ import unicode_literals

import io
import optparse
import re

from utils import read_file, write_file


def main():
parser = optparse.OptionParser(usage='%prog INFILE OUTFILE')
Expand All @@ -14,8 +15,7 @@ def main():

infile, outfile = args

with io.open(infile, encoding='utf-8') as inf:
readme = inf.read()
readme = read_file(infile)

bug_text = re.search(
r'(?s)#\s*BUGS\s*[^\n]*\s*(.*?)#\s*COPYRIGHT', readme).group(1)
Expand All @@ -25,8 +25,7 @@ def main():

out = bug_text + dev_text

with io.open(outfile, 'w', encoding='utf-8') as outf:
outf.write(out)
write_file(outfile, out)


if __name__ == '__main__':
Expand Down
17 changes: 7 additions & 10 deletions devscripts/make_issue_template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env python
from __future__ import unicode_literals

import io
import optparse
import os.path
import sys

from utils import read_file, read_version, write_file


def main():
Expand All @@ -13,17 +16,11 @@ def main():

infile, outfile = args

with io.open(infile, encoding='utf-8') as inf:
issue_template_tmpl = inf.read()

# Get the version from youtube_dl/version.py without importing the package
exec(compile(open('youtube_dl/version.py').read(),
'youtube_dl/version.py', 'exec'))
issue_template_tmpl = read_file(infile)

out = issue_template_tmpl % {'version': locals()['__version__']}
out = issue_template_tmpl % {'version': read_version()}

with io.open(outfile, 'w', encoding='utf-8') as outf:
outf.write(out)
write_file(outfile, out)

if __name__ == '__main__':
main()
11 changes: 7 additions & 4 deletions devscripts/make_readme.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import unicode_literals

import io
import sys
import os.path
import re
import sys
dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

from utils import read_file
from youtube_dl.compat import compat_open as open

README_FILE = 'README.md'
Expand All @@ -12,8 +16,7 @@
if isinstance(helptext, bytes):
helptext = helptext.decode('utf-8')

with io.open(README_FILE, encoding='utf-8') as f:
oldreadme = f.read()
oldreadme = read_file(README_FILE)

header = oldreadme[:oldreadme.index('# OPTIONS')]
footer = oldreadme[oldreadme.index('# CONFIGURATION'):]
Expand Down
15 changes: 8 additions & 7 deletions devscripts/make_supportedsites.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env python
from __future__ import unicode_literals

import io
import optparse
import os
import os.path
import sys


# Import youtube_dl
ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, ROOT_DIR)
dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

import youtube_dl

from utils import write_file


def main():
parser = optparse.OptionParser(usage='%prog OUTFILE.md')
Expand All @@ -38,8 +40,7 @@ def gen_ies_md(ies):
' - ' + md + '\n'
for md in gen_ies_md(ies))

with io.open(outfile, 'w', encoding='utf-8') as outf:
outf.write(out)
write_file(outfile, out)


if __name__ == '__main__':
Expand Down
Loading

2 comments on commit b870181

@nicolaasjan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while running make:

mkdir -p zip
for d in youtube_dl youtube_dl/downloader youtube_dl/extractor youtube_dl/postprocessor ; do \
  mkdir -p zip/$d ;\
  cp -pPR $d/*.py zip/$d/ ;\
done
touch -t 200001010101 zip/youtube_dl/*.py zip/youtube_dl/*/*.py
mv zip/youtube_dl/__main__.py zip/
cd zip ; zip -q ../youtube-dl youtube_dl/*.py youtube_dl/*/*.py __main__.py
rm -rf zip
echo '#!/usr/bin/env python' > youtube-dl
cat youtube-dl.zip >> youtube-dl
rm youtube-dl.zip
chmod a+x youtube-dl
COLUMNS=80 /usr/bin/env python youtube_dl/__main__.py --help | /usr/bin/env python devscripts/make_readme.py
/usr/bin/env python devscripts/make_contributing.py README.md CONTRIBUTING.md
pandoc -f markdown-smart -t plain README.md -o README.txt
/usr/bin/env python devscripts/prepare_manpage.py youtube-dl.1.temp.md
pandoc -s -f markdown-smart -t man youtube-dl.1.temp.md -o youtube-dl.1
rm -f youtube-dl.1.temp.md
/usr/bin/env python devscripts/bash-completion.py
/usr/bin/env python devscripts/zsh-completion.py
/usr/bin/env python devscripts/fish-completion.py
Traceback (most recent call last):
  File "devscripts/fish-completion.py", line 50, in <module>
    build_completion(parser)
  File "devscripts/fish-completion.py", line 46, in build_completion
    write_file(filled_template)
TypeError: write_file() missing 1 required positional argument: 'content'
make: *** [Makefile:110: youtube-dl.fish] Fout 1

@dirkf
Copy link
Contributor Author

@dirkf dirkf commented on b870181 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks:

--- a/devscripts/fish-completion.py
+++ b/devscripts/fish-completion.py
@@ -43,7 +43,7 @@ def build_completion(opt_parser):
 
     template = read_file(FISH_COMPLETION_TEMPLATE)
     filled_template = template.replace('{{commands}}', '\n'.join(commands))
-    write_file(filled_template)
+    write_file(FISH_COMPLETION_FILE, filled_template)
 

Please sign in to comment.