Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions etc/scripts/freeze_and_update_reqs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# ScanCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from __future__ import absolute_import
from __future__ import print_function


import fnmatch
import hashlib
import io
import os
import re
import subprocess


def is_package_exist_with_hashes(package_name,hash_of_file):
""" Check if any line in the file contains package with same hashes """
# Open the file in read only mode
line_with_hash = "{} \\\n --hash=sha256:{}".format(package_name,hash_of_file)
with open('requirements.txt') as file:
# Read all lines in the file one by one
for line in file:
# For each line, check if line contains the package with same hashes
if line_with_hash in line:
return True
return False

def is_package_exist_without_hashes(package_name):
""" Check if any line in the file contains package without same hash """
# Open the file in read only mode
line_with_hash = "{} \\\n".format(package_name)
with open('requirements.txt') as file:
# Read all lines in the file one by one
for line in file:
# For each line, check if line contains the string
if line_with_hash in line:
return True
return False


def add_package(package_name,hash_of_file):
with open('requirements.txt', 'a') as file:
# Append package with hashes at the end of file
line = "{} \\\n --hash=sha256:{}\n".format(package_name,hash_of_file)
file.write(line)

def append_requirement_file(package_name,hash_of_file):
if is_package_exist_with_hashes(package_name,hash_of_file):
return
else:
inputfile = open('requirements.txt').readlines()
write_file = open('requirements.txt','w')
for line in inputfile:
write_file.write(line)
current_line = "{} \\\n".format(package_name)
if current_line in line:
new_line = " --hash=sha256:{} \\".format(hash_of_file)
write_file.write(new_line + "\n")
write_file.close()


def read_chunks(file, size=io.DEFAULT_BUFFER_SIZE):
"""Yield pieces of data from a file-like object until EOF."""
while True:
chunk = file.read(size)
if not chunk:
break
yield chunk

def hash_of_file(path):
# type: (str) -> str
"""Return the hash digest of a file."""
with open(path, 'rb') as archive:
hash = hashlib.new('sha256')
for chunk in read_chunks(archive):
hash.update(chunk)
return hash.hexdigest()

def main():
os.chdir(os.pardir) #go to etc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should accept an argument with a path to a requirements files instead of trying to change the working dir.

os.chdir(os.pardir) # go to scancode-toolkit
cwd = os.getcwd()
print("Current working directory:", cwd)
fp= open('requirements.txt','w') #Create empty file because it must exits before opening the file in read mode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use a with context manager.

for subdir, dirs, files in os.walk(cwd+"/thirdparty"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should accept an argument for the thirdparty directory. Also it would be likely easier to understand if you load all file paths at once and iterate after. You should also reuse the commonode.fileutils functions

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You talking about which function in fileutils?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

resource_iter()

for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith(".whl") and (fnmatch.fnmatchcase(filename, "*py3*") or fnmatch.fnmatchcase(filename, "*cp36*")):
name = filename.split('-')[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would really prefer that you use that https://github.com/jwodder/wheel-filename as a library.

version = filename.split('-')[1]
package_name = name + "==" + version
hs= hash_of_file(filepath)
if is_package_exist_without_hashes(package_name):
append_requirement_file(package_name,hs)
else: add_package(package_name,hs)

elif filepath.endswith(".gz") and not filename.startswith("py2-"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

which filename would ever start with "py2-"? and what is endswith(".gz")? about?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

py2-ipaddress.tar.gz

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why would you single out a specific package name? What's so special about this?

@Abhishek-Dev09-zz Abhishek-Dev09-zz Jul 8, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

because my function would not work for py2-ipaddress.tar.gz but don't worry this package will be removed after dropping python 2.

@pombredanne pombredanne Jul 9, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

your function should not have any Python-package specific hack. The problem is in your code then

name = filename.split('-')[0]
version = re.findall("\d+\.\d+\.+\d|\d+\.\d+\d*|\d+\d*",filename)
if len(version)>=2:
version[0]= version[0]+version[1]
if re.search(r'\d', name):
version[0]= version[1]
hs= hash_of_file(filepath)
package_name = name + "==" + version[0]
if is_package_exist_without_hashes(package_name):
append_requirement_file(package_name,hs)
else: add_package(package_name,hs)
fp.close()

if __name__ == '__main__':
main()
56 changes: 56 additions & 0 deletions github_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# ScanCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from __future__ import absolute_import
from __future__ import print_function

import fnmatch
import os
import subprocess


def main():
cwd = os.getcwd()
print("Current working directory:", cwd)
subprocess.run(["pip3","install","github-release-retry"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do not do that. Have a requirements file instead and a README.rst that explains what to do

token = input ("Enter GITHUB_TOKEN :")

os.environ ["GITHUB_TOKEN"] = token

tag = input ("Enter tag_name :")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do not use input, instead use arguments for main() and accept command line arguments

repo = input ("Enter name of repository name :")
body = input ("Enter body string :")
user = input ("Enter Github user name :")
limit = input ("Enter retry limit :")
for subdir, dirs, files in os.walk(cwd+"/thirdparty"):
for filename in files:
if fnmatch.fnmatchcase(filename, "*py3*") or fnmatch.fnmatchcase(filename, "*cp36*") or (fnmatch.fnmatchcase(filename, "*tar.gz*") and not fnmatch.fnmatchcase(filename, "*py2*")):
filepath = subdir + os.sep + filename
subprocess.run(["python3","-m","github_release_retry.github_release_retry","--user",user,"--repo",repo,"--tag_name",tag,"--body_string",body,"--retry_limit",limit,filepath])

if __name__ == '__main__':
main()

Binary file removed thirdparty/Sphinx-1.8.5-py2.py3-none-any.whl
Binary file not shown.
21 changes: 0 additions & 21 deletions thirdparty/Sphinx-1.8.5-py2.py3-none-any.whl.ABOUT

This file was deleted.

Binary file removed thirdparty/more_itertools-4.2.0-py3-none-any.whl
Binary file not shown.
Binary file added thirdparty/pyahocorasick-1.4.0.tar.gz
Binary file not shown.
Binary file removed thirdparty/spdx_tools-0.6.0-py2.py3-none-any.whl
Binary file not shown.
22 changes: 0 additions & 22 deletions thirdparty/spdx_tools-0.6.0-py2.py3-none-any.whl.ABOUT

This file was deleted.

10 changes: 0 additions & 10 deletions thirdparty/spdx_tools-0.6.0-py2.py3-none-any.whl.NOTICE

This file was deleted.

Binary file removed thirdparty/url-0.1.4.5-py2-none-any.whl
Binary file not shown.
Binary file removed thirdparty/url-0.1.4.5-py3-none-any.whl
Binary file not shown.