streamlit
1#!/usr/bin/env python
2
3# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Update project name across the entire repo.
18
19The streamlit-nightly CI job uses this to set the project name to "streamlit-nightly".
20"""
21
22import fileinput23import os24import re25import sys26from typing import Dict27
28BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))29
30# A dict of [filename:regex]. For each filename, we modify all lines
31# matched by the regex.
32#
33# Regexes should start with a "<pre_match>" named group and end with a
34# "<post_match>" named group. Text between these pre- and post-match
35# groups will be replaced with the specified project_name text.
36FILES_AND_REGEXES = {37"lib/setup.py": r"(?P<pre_match>.*name=\").*(?P<post_match>\")",38"lib/streamlit/version.py": r"(?P<pre_match>.*_version\(\").*(?P<post_match>\"\)$)",39}
40
41
42def update_files(project_name: str, files: Dict[str, str]) -> None:43"""Update files with new project name."""44for filename, regex in files.items():45filename = os.path.join(BASE_DIR, filename)46matched = False47pattern = re.compile(regex)48for line in fileinput.input(filename, inplace=True):49line = line.rstrip()50if pattern.match(line):51line = re.sub(52regex, rf"\g<pre_match>{project_name}\g<post_match>", line53)54matched = True55print(line)56if not matched:57raise Exception(f'In file "{filename}", did not find regex "{regex}"')58
59
60def main() -> None:61if len(sys.argv) != 2:62raise Exception(f'Specify project name, e.g: "{sys.argv[0]} streamlit-nightly"')63project_name = sys.argv[1]64update_files(project_name, FILES_AND_REGEXES)65
66
67if __name__ == "__main__":68main()69